package com.xu.controller;

import com.github.pagehelper.PageInfo;
import com.xu.pojo.College;
import com.xu.pojo.Course;
import com.xu.pojo.UserDto;
import com.xu.service.CollegeService;
import com.xu.service.CourseService;
import com.xu.service.SelectService;
import com.xu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class StudentController {



    @Autowired
    @Qualifier("collegeServiceImpl")
    private CollegeService collegeService;

    @Autowired
    @Qualifier("courseServiceImpl")
    private CourseService courseService;

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("selectServiceImpl")
    private SelectService selectService;

    private final int pageSize = 5;


    @RequestMapping("/stu/queryCourse")
    public String queryCourse(Model model){
        List<College> colleges = collegeService.queryAllCollegeExceptOffice();
        PageInfo<Course> courses = selectService.SetStock(courseService.queryCoursePaging(1, pageSize));
        int selectState = selectService.getSelectState();
        long remainSeconds = selectService.getremainSeconds();
        model.addAttribute("courses",courses);
        model.addAttribute("colleges",colleges);
        model.addAttribute("selectState",selectState);
        model.addAttribute("remainSeconds",remainSeconds);
        return "stu/select";
    }


    @PostMapping("/stu/queryCourseIf")
    @ResponseBody
    public PageInfo<Course> queryCourseIf(String queryCondition,int collegeId,int pageNum){
        PageInfo<Course> courses = selectService.SetStock(courseService.queryCourseIfPaging(queryCondition, collegeId, pageNum, pageSize));

        return courses;
    }


    @PostMapping("/stu/selectCourse")
    @ResponseBody
    public String selectCourse(int courseId){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        return selectService.selectCourse(userDto.getUserId(),courseId);
    }

    @RequestMapping("/stu/querySelect")
    public String querySelect(Model model){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        List<Course> courses = selectService.getUserCourse(userDto.getUserId());
        int selectState = selectService.getSelectState();
        model.addAttribute("courses", courses);
        model.addAttribute("selectState", selectState);
        return "stu/myselect";
    }

    @RequestMapping("/stu/deleteSelect")
    @ResponseBody
    public String deleteSelect(int courseId){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        return selectService.dropCourse(userDto.getUserId(),courseId);
    }
}
